var common = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sum: 1,
    type: 'edit',
    select: [],
    carList: [],
    allPrice:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: common.baseUrl
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
    let array = wx.getStorageSync('carList') || []
    this.setData({
      carList: array
    })
  },

  // 编辑切换
  toggleType(e) {
    this.setData({
      type: e.currentTarget.dataset.type,
      select: [],
      selectSum: 0,
      allPrice: 0,
      all:false
    })
  },

  // 删除商品
  handleDelect() {
    let select = this.data.select
    let array = wx.getStorageSync('carList') || []
    if (select.length > 0) {
      var _that = this
      wx.showModal({
        title: '确认删除宝贝吗？',
        confirmColor: "#E7211A",
        cancelColor: "#666666",
        success(res) {
          if (res.confirm) {
            select.forEach(function (item, index, select) {
              if (item >= 0 && item != null) {
                for(let i = 0;i<array.length;i++){
                  if(item == array[i].goods_id){
                    array.splice(i,1)
                  }
                }
              }
            })
            _that.reSet(array)
            _that.setData({
              select: [],
              selectSum: 0
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }

  },

  //结算
  handleGoSettle() {
    let array = []
    let select = this.data.select
    if(this.data.allPrice > 0){
      select.forEach(function(item){
        if(item >=0 && item != null){
          array.push(item)
        }
      })
      wx.navigateTo({
        url: '../settle/settle?array=' + JSON.stringify(array) + '&price=' + this.data.allPrice,
      })
    }

  },

  //点击选择
  handleSelect(e) {
    let array = this.data.select
    let item = e.currentTarget.dataset.item
    let id = item.goods_id
    let index = e.currentTarget.dataset.index
    array[index] = id
    this.selectInfo(array)
    if(this.data.type == 'edit'){
      this.handleAllPrice()
    }
  },

  //点击取消选择
  handleNoSelect(e) {
    let array = this.data.select
    let index = e.currentTarget.dataset.index
    array[index] = null
    this.selectInfo(array)
    if(this.data.type == 'edit'){
      this.handleAllPrice()
    }
  },

  //全选
  handleAllSelect() {
    let array = this.data.carList
    let select = []
    let selectSum = 0
    array.forEach(function (item) {
      select.push(item.goods_id)
      selectSum = selectSum + 1
    })
    this.setData({
      all: true,
      select: select,
      selectSum: selectSum
    })
    if(this.data.type == 'edit'){
      this.handleAllPrice()
    }
  },

  //取消全选
  handleNoAllSelect() {
    this.setData({
      all: false,
      select: [],
      selectSum: 0,
      allPrice: 0
    })
  },

  selectInfo(data) {
    let array = data
    let selectSum = 0
    array.forEach(function (item) {
      if (item >= 0 && item != null) {
        selectSum = selectSum + 1
      }
    })
    this.setData({
      select: array,
      selectSum: selectSum,
      all: false
    })
  },
  
  //合计金额
  handleAllPrice(){
    let array = wx.getStorageSync('carList') || []
    let select = this.data.select
    let allPrice = 0
    let _that = this
    select.forEach(function(item){
      if(item >= 0 && item != null){
        for(let i = 0;i<array.length;i++){
          if(item == array[i].goods_id){
            let price = parseFloat(array[i].price)
            let account = array[i].account
            let n = common.accMul(price,account)
            allPrice = common.accAdd(allPrice,n)
            _that.setData({
              allPrice : allPrice
            })
          }
        }
      }
    })
  },


  // 操作减数量
  handleDecrease(e) {
    let array = wx.getStorageSync('carList') || []
    let info = e.currentTarget.dataset.item
    let sum = info.account
    sum = sum - 1
    this.handleCoc(array, sum, info)
  },
  // 操作加数量
  handleAdd(e) {
    let array = wx.getStorageSync('carList') || []
    let info = e.currentTarget.dataset.item
    let sum = info.account
    let quantity = parseInt(info.quantity);
    if (sum < quantity) {
      sum = sum + 1,
        this.handleCoc(array, sum, info)
    } else {
      common.showToast('没有库存了！')
    }
  },
  // 重新缓存
  handleCoc(array, sum, info) {
    if (array.length > 0) {
      array.forEach(function (item, index, array) {
        if (info.goods_id == item.goods_id) {
          info.account = sum
          if (info.account <= 0) {
            array.splice(index, 1)
          } else {
            array[index] = info
          }
        }
      })
    }
    this.reSet(array)
  },
  reSet(array){
    var that = this
    wx.setStorage({
      key: "carList",
      data: array,
      success() {
        that.setData({
          carList: array
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
})